package com.yu.scanner;

import com.yu.annotation.RpcReference;
import com.yu.provider.ServiceProviderImpl;
import com.yu.annotation.RpcService;
import com.yu.factory.SingletonFactory;
import com.yu.provider.ServiceProvider;
import com.yu.proxy.RpcClientProxy;
import com.yu.service.RpcServiceProperties;
import com.yu.transport.client.NettyClient;
import com.yu.transport.client.RpcRequestTransport;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;

/**
 * @PackageName: com.yu.scanner
 * @Description: BeanPostProcessor会在bean进行实例化的时候调用，在构造函数执行完和实例化结束之后分别调用
 * @Version: v1.0.0
 * @Author: yuweilin
 * @Date: 13:23
 * <p>
 * Modification History:
 * Date         Author          Version            Description
 * ------------------------------------------------------------
 * 2021/1/22      yuweilin         v1.0.0               新建
 */
@Slf4j
@Component
public class SpringBeanProcessor implements BeanPostProcessor {

    private final ServiceProvider serviceProvider;
    private final RpcRequestTransport transport;

    public SpringBeanProcessor() {
        this.serviceProvider = SingletonFactory.getInstance(ServiceProviderImpl.class);
        this.transport = SingletonFactory.getInstance(NettyClient.class);
    }

    /**
     * 在bean构造函数执行后，带有rpcService接口，那么将服务同时发布到zk服务器上
     * 服务端
     * @param bean
     * @param beanName
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (bean.getClass().isAnnotationPresent(RpcService.class)){
            log.info("{} with Annotation {}",bean.getClass().getName(),RpcService.class.getCanonicalName());
            RpcService service = bean.getClass().getAnnotation(RpcService.class);
            RpcServiceProperties rpcServiceProperties = RpcServiceProperties.builder().group(service.group()).version(service.version()).build();
            serviceProvider.publishService(bean,rpcServiceProperties);
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        Class<?> targetClass = bean.getClass();
        Field[] declaredFields = targetClass.getDeclaredFields();
        for (Field field : declaredFields) {
            RpcReference reference = field.getAnnotation(RpcReference.class);
            if (reference!=null){
                RpcServiceProperties rpcServiceProperties = RpcServiceProperties.builder()
                        .group(reference.group()).version(reference.version()).build();
                RpcClientProxy proxy = new RpcClientProxy(transport, rpcServiceProperties);
                Object clientProxy = proxy.getProxy(field.getType());
                field.setAccessible(true);
                try {
                    field.set(bean,clientProxy);
                }catch (IllegalAccessException e){
                    e.printStackTrace();
                }
            }
        }
        return bean;

    }
}
